/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.dialog.jspgeneration;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.message.Messages;
import com.genia.toolbox.web.gwt.form.client.form.Form;

/**
 * JSP Generation dialog.
 */
@SuppressWarnings("serial")
public class JSPGenerationDialog
    extends JDialog
    implements ActionListener
{

  /**
   * The application gui.
   */
  private FormEditorGUI gui;

  /**
   * The forms label.
   */
  private JLabel labelForm = null;

  /**
   * The comment label.
   */
  private JTextArea labelComment = null;

  /**
   * The forms list.
   */
  private JComboBox comboForm = null;

  /**
   * The cancel button.
   */
  private JButton buttonCancel = null;

  /**
   * The generation button.
   */
  private JButton buttonGenerate = null;

  /**
   * The generate all button.
   */
  private JButton buttonGenerateAll = null;

  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   */
  public JSPGenerationDialog(FormEditorGUI gui)
  {
    super(gui, true);
    this.gui = gui;
    this.initialise();
  }



  /**
   * Initialise the dialog.
   */
  public void initialise()
  {
    this.setTitle(this.gui.getController().notifyTranslation(Messages.JSP_GENERATION_TITLE_DIALOG_KEY));

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int width = 350;
    int height = 175;
    this.setBounds((dim.getSize().width - width) / 2, (dim.getSize().height - height) / 2, width, height);

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    this.getContentPane().setLayout(layout);

    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 5, 5, 5);
    this.getContentPane().add(this.createMainPanel(), constraints);

    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = new Insets(0, 5, 5, 5);
    constraints.anchor = GridBagConstraints.EAST;
    this.getContentPane().add(this.createButtonPanel(), constraints);
    
  }



  /**
   * Create the main panel.
   * 
   * @return The main panel.
   */
  public JPanel createMainPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    panel.setBorder(BorderFactory.createTitledBorder(this.getGui().getController().notifyTranslation(Messages.JSP_GENERATION_BORDER_TITLE_DIALOG_KEY)));

    this.labelComment = new JTextArea(this.getGui().getController().notifyTranslation(Messages.JSP_GENERATION_FORM_LABEL_COMMENT_DIALOG_KEY));
    this.labelComment.setFont(this.getFont());
    this.labelComment.setBackground(this.getBackground());
    this.labelComment.setEditable(false);
    this.labelComment.setLineWrap(true);
    this.labelComment.setWrapStyleWord(true);
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 5, 5);
    panel.add(this.labelComment, constraints);

    this.labelForm = new JLabel(this.getGui().getController().notifyTranslation(Messages.JSP_GENERATION_FORM_LABEL_DIALOG_KEY));
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = new Insets(0, 5, 5, 5);
    panel.add(this.labelForm, constraints);

    List<Form> forms = this.getGui().getController().notifyDisplayForms();
    ComboBoxModel model = new DefaultComboBoxModel(forms.toArray());
    ListCellRenderer renderer = new BasicComboBoxRenderer()
    {
      @Override
      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
      {
        String formName = "";
        Form form = (Form) value;
        if (form != null) {
          formName = form.getName();
        }
        return super.getListCellRendererComponent(list, formName, index, isSelected, cellHasFocus);
      }
    };
    this.comboForm = new JComboBox(model);
    this.comboForm.setRenderer(renderer);
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 5, 5);
    panel.add(this.comboForm, constraints);

    return panel;
  }



  /**
   * Create the panel containing the buttons.
   * 
   * @return The button panel.
   */
  public JPanel createButtonPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    this.buttonCancel = new JButton(this.gui.getController().notifyTranslation(Messages.JSP_GENERATION_CANCEL_BUTTON_DIALOG_KEY));
    this.buttonCancel.addActionListener(this);
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(this.buttonCancel, constraints);

    this.buttonGenerate = new JButton(this.gui.getController().notifyTranslation(Messages.JSP_GENERATION_GENERATE_BUTTON_DIALOG_KEY));
    this.buttonGenerate.addActionListener(this);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 5, 0, 0);
    panel.add(this.buttonGenerate, constraints);
    
    this.buttonGenerateAll = new JButton(this.gui.getController().notifyTranslation(Messages.JSP_GENERATION_GENERATE_ALL_BUTTON_DIALOG_KEY));
    this.buttonGenerateAll.addActionListener(this);
    constraints.gridx = 3;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 5, 0, 0);
    panel.add(this.buttonGenerateAll, constraints);

    return panel;

  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source.equals(this.buttonCancel)) {
      this.dispose();
    }
    else if (source.equals(this.buttonGenerate)) {
      Form form = (Form) this.comboForm.getSelectedItem();
      if (form != null) {
        this.getGui().getController().notifyExportToJsp(form);
        this.dispose();
      }
    }
    else if (source.equals(this.buttonGenerateAll)) {
      List<Form> forms = new ArrayList<Form>();
      
      for (int i=0; i<this.comboForm.getItemCount(); i++) {
        Form form = (Form) this.comboForm.getItemAt(i);
        if (form != null) {
          forms.add(form);
        }
      }
      
        this.getGui().getController().notifyExportAllToJsp(forms);
        this.dispose();
      
    }
  }



  /**
   * Get the gui.
   * 
   * @return the gui.
   */
  public FormEditorGUI getGui()
  {
    return this.gui;
  }

}
